/*
 * @lc app=leetcode.cn id=91 lang=cpp
 *
 * [91] 解码方法
 */

// @lc code=start
class Solution {
public:
    int numDecodings(string s) {
        const int n = s.size();
        if(n == 0) return 0;
        if(n == 1) return s[0] == '0' ? 0 : 1;
        
        vector<int> dp(n+1, 0);
        dp[0] = 1;
        dp[1] = s[0] == '0' ? 0 : 1;
        
        for(int i = 2; i <= n; i++)
        {
            if(s[i-1] != '0')
            {
                dp[i] += dp[i-1];
            }
            if(s[i-2] == '1' || (s[i-2] == '2' && s[i-1] <= '6'))
            {
                dp[i] += dp[i-2];
            }
        }
        
        return dp[n];
    }
};
// @lc code=end

